package com.car.postoffice;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.io.UnsupportedEncodingException;

public class CMailReply implements Serializable {
	private static final long serialVersionUID = 1L;
	private CMailHeader header;
	private CMailResult result;
	
	/*
	 *  回复信息可以是普通的字节数组，也可以是相应可序列化的对象类型
	 *  目的： 能够让异步的http等数据层线程，帮助进行图像解码等等操作
	 */
	private byte[] mailContent;
	private Serializable mailObj;

	public CMailReply(){	
	}
	
	public CMailHeader getHeader() {
		if(header == null){
			return new CMailHeader(null);
		}
		return header;
	}
	public void setHeader(CMailHeader header) {
		this.header = header;
	}
	
	public byte[] getMailContent() {
		if(mailContent == null){
			return new byte[0];
		}
		return mailContent;
	}
	public void setMailContent(byte[] mailContent) {
		this.mailContent = mailContent;
	}
	
	public CMailResult getResult() {
		if(result == null){
			return new CMailResult(0, "");
		}
		return result;
	}
	public void setResult(CMailResult result) {
		this.result = result;
	}
	
	public Serializable getMailObj() {
		return mailObj;
	}

	public void setMailObj(Serializable mailObj) {
		this.mailObj = mailObj;
	}
	
	@Override
	public String toString(){
		StringBuffer buffer = new StringBuffer(256);
		buffer.append("{\n");
		if(header != null){
			buffer.append(header.toString());
		}
		if(result != null){
			buffer.append(result.toString());
		}
		
		if(mailContent != null){
			buffer.append("\n\n");
			try {
				buffer.append(new String(mailContent, "utf-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			buffer.append("\n");
		}
		
		if(mailObj != null){
			buffer.append("\n\n");
			buffer.append(mailObj);
			buffer.append("\n");
		}
		buffer.append("}\n");
		return buffer.toString();
	}
	
	public static void main(String[] args){
		CMailReply reply = new CMailReply();
		CMailHeader header = new CMailHeader(null);
		CMailResult result = new CMailResult(123, "test");
		header.setMailId(123);
		header.setParameter("haha", "en");
		header.setMailType("Image");
		reply.setHeader(header);
		reply.setMailContent(new String("nothing").getBytes());
		reply.setResult(result);
		reply.setMailObj(new String("this is a mail object"));
		
		try {
			String filename = "F:\\android-sdk-windows\\TestFiles\\MailReply.txt";
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream(filename));
			
			out.writeObject(reply);
			out.flush();
			out.close();
			
			ObjectInputStream in = new ObjectInputStream(
					new FileInputStream(filename));
			try {
				CMailReply objReply = (CMailReply)in.readObject();
				System.out.println(objReply.toString());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
